package cn.tedu.controller;

import cn.tedu.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author JokerQiu
 * @date 2021年12月16日 09:13
 * 验证邮箱
 */
@WebServlet("/checkEmail")
public class CheckEmailServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 1.获取参数
         * 2.验证email是否存在
         * 3.返回数据
         */
        req.setCharacterEncoding("UTF-8");
        //1、获取参数
        String email = req.getParameter("email");
        // 2.验证email是否存在
        boolean b = userService.checkEmail(email);
        //3.返回数据
        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();
        if (b) {
            // 邮箱可用
            writer.write("1");
        } else {
            // 邮箱不可用
            writer.write("2");
        }
    }
}
